const express = require('express');
const mongoose = require('mongoose');
// 连接mongo
const DB_URL = 'mongodb://localhost:27017/imooc';
mongoose.connect(DB_URL);
mongoose.connection.on('connected',function(){
    console.log('数据库连接成功')
})

const User = mongoose.model('user',new mongoose.Schema({
    name:{type:String,require:true},
    age:{type:Number,require:true}
}))

/* User.create({
    name:'imooc',
    age:18
},function(err,doc){
    if(!err){
        console.log(doc);
    }else{
        console.log(err)
    }
}) */

//  新建app
const app = express();

app.get('/',function(req,res){
   
    res.send('hello')
})

app.get('/data',function(req,res){
    User.find({},function(err,doc){
        res.json(doc)
    })
})

app.listen(8000,function(){
    console.log('监听8000端口')
})